package com.oros.db;
import java.text.*;
import java.util.Date;

import com.vaadin.data.Property.ConversionException;
public class AEDateTimeFormatter implements AEFieldFormatter {

	private static final long serialVersionUID = 5309350135110856207L;
	private SimpleDateFormat formatter;
	private String columnName;
	private int columnNr;
	private Class<?> type;
	private int specDays;
	private long specWeight=3600000L*24;
	private boolean nullable;
	private String format;
    public AEDateTimeFormatter(String columnName,String format,boolean nullable)
    {
       specDays=0;
       this.nullable = nullable;
       this.format = format;
       int n=format.length();
       for(int i=0;i<n;i++) if (format.charAt(i)=='v') specDays++;
       else break;
       if (specDays==0) 
       {
    	   for(int i=0;i<n;i++) if (format.charAt(i)=='c') specDays++;
           else break;
    	   if (specDays>0) specWeight=3600000;
       }
       if (specDays>0) format=format.substring(specDays);
       formatter = new SimpleDateFormat(format);
       this.columnName = columnName;
       columnNr = -1;
     }
    public AEDateTimeFormatter(String columnName,String format)
    {
    	this(columnName,format,false);
    }
	@Override
	public Object generateCell(AETableContainer table,AERawItem item,int columnNo,int action)
	{
		if (columnNr<0)
		{
			columnNr=table.getColumnIndex(columnName);
			if (columnNr<0) 
				throw new RuntimeException("Unknown column name: "+columnName+" in "+table);
			type = table.getType(columnName);
		}
		Object data = item.getColumnValue(columnNr);
		if (data==null)
		{
			if (nullable) return "";
			data = new Long(0);
		}
		Date d;
		if (data instanceof Date)
			d=(Date)data;
		else if (data instanceof Long)
			d=new Date(((Long)data).longValue());
		else if (data instanceof Integer)
			d=new Date(((Integer)data).longValue());
		else 
		{
			long l;
			try
			{
			  l = Long.parseLong(data.toString());
			} catch(Exception e)
			{
				l=0;
			}
			d = new Date(l);
		}
		StringBuilder res=new StringBuilder();
		if (specDays>0)
		{
			long v=d.getTime();
			long vdays = v / (specWeight);
			v -= vdays * specWeight;
			d = new Date(v);
			String pref=Long.toString(vdays);
			int n= specDays-pref.length();
			while ((n--)>0) res.append('0');
			res.append(pref);
		}
		res.append(formatter.format(d));
		return res.toString();
	}

	@Override
	public Object setField(AERawItem item,AEColumnProperty column,Object value) throws ConversionException {
		Date d;String s=value.toString().trim();long addVal=0;
		if (s.length()==0)
		{
			if (nullable)
			{
				item.setPropertyOfItem(columnNr, null);
				return "";
			}
			throw new ConversionException("The field must be filled in by the format: "+format);
		}
		try
		{
			if (specDays>0)
			{
				int n=s.length();
				int k=0;char c;
				while (k<n && (c=s.charAt(k))>='0' && c<='9') k++;
				if (k>0)
				{
					addVal=Long.parseLong(s.substring(0, k))*specWeight;
					s=s.substring(k);
				}
			}
		    d = formatter.parse(s);
		}
		catch(Exception e)
		{
			throw new ConversionException(value.toString());
		}
		if (addVal>0)
		{
			d = new Date(d.getTime()+addVal);
		}
		Object dat=d;
		//System.out.println("value="+value+" data="+d+" type="+type.getCanonicalName());
		if (type!=java.util.Date.class)
		{
			if (type==Long.class)
			{
				dat =new Long(d.getTime());
			}
			else if (type==Integer.class)
			{
				dat = new Integer((int)d.getTime());
			}
			else
			{
				dat = Long.toString(d.getTime());
			}
		}
		item.setPropertyOfItem(columnNr, dat);
		return value;
	}

}
